# uncompyle6 version 3.2.3
# Python bytecode 3.6 (3379)
# Decompiled from: Python 3.6.8 |Anaconda custom (64-bit)| (default, Feb 21 2019, 18:30:04) [MSC v.1916 64 bit (AMD64)]
# Embedded file name: helpers\mailer\base_mailer.py
import logging, datetime
from dateutil.tz import tzlocal
from sqlalchemy import select as sql_select
from helpers.mailer.sender.base import Sender
from helpers.mailer.renderer.base import Renderer
from db import Connection
from db.tables.users import UserRow, UsersTable

_logger = logging.getLogger("helpers.mailer")


class Mailer:
    def __init__(self, shard, sender, renderer):
        self.shard = shard
        self.sender = sender
        self.renderer = renderer

    def enqueue(
        self,
        from_address,
        data,
        user_id=None,
        subject=None,
        shard=None,
        notification_type=None,
        subject_template=None,
        html_template=None,
        txt_template=None,
        frontend_url=None,
        email=None,
        first_name=None,
        last_name=None,
    ):
        if self.sender.can_send():
            pass
        try:
            self.send(
                from_address=from_address,
                user_id=user_id,
                data=data,
                subject=subject,
                shard=shard,
                notification_type=notification_type,
                subject_template=subject_template,
                html_template=html_template,
                txt_template=txt_template,
                frontend_url=frontend_url,
                email=email,
                first_name=first_name,
                last_name=last_name,
            )
        except Exception as e:
            _logger.exception("exception during sending email %s", e)

    def send(
        self,
        from_address,
        data,
        user_id=None,
        subject=None,
        shard=None,
        notification_type=None,
        subject_template=None,
        html_template=None,
        txt_template=None,
        frontend_url=None,
        email=None,
        first_name=None,
        last_name=None,
    ):
        if not self.sender.can_send():
            return
        notifications = {}
        owner_id = None
        if user_id is not None:
            owner_table = UsersTable.alias("owner_table")
            from_clause = UsersTable
            from_clause = from_clause.outerjoin(
                owner_table, owner_table.c.user_id == UsersTable.c.owner_id
            )
            query = (
                sql_select(
                    (
                        UserRow.email,
                        UserRow.first_name,
                        UserRow.last_name,
                        UserRow.notifications,
                        UserRow.deleted_at,
                        owner_table.c.user_id.label("owner_idl"),
                        owner_table.c.email.label("owner_email"),
                        owner_table.c.first_name.label("owner_first_name"),
                        owner_table.c.last_name.label("owner_last_name"),
                        owner_table.c.notifications.label("owner_notifications"),
                        owner_table.c.deleted_at.label("owner_deleted_at"),
                    )
                )
                .select_from(from_clause)
                .where(UserRow.user_id == user_id)
            )
            if shard is None:
                shard = self.shard
            with Connection(shard) as (connection):
                user_data = connection.execute(query).fetchone()
                if user_data is None:
                    raise Exception("user not found %s" % (user_id,))
                if user_data.deleted_at:
                    owner_id = user_data.owner_id
                    if owner_id is None:
                        raise Exception("owner not found %s" % (user_id,))
                    if user_data.owner_deleted_at:
                        raise Exception("user deleted %s" % (user_id,))
                    email = user_data.owner_email
                    first_name = user_data.owner_first_name
                    last_name = user_data.owner_last_name
                    notifications = user_data.owner_notifications
                else:
                    email = user_data.email
                    first_name = user_data.first_name
                    last_name = user_data.last_name
                    notifications = user_data.notifications
        else:
            if email is None:
                raise Exception("no destination provided")
        if notifications is None:
            notifications = {}
        if notification_type == "scans_notification":
            if notifications.get("scans", False):
                _logger.debug("scans_notification skipped by settings")
                return
            mail_data = dict(
                owner_id=owner_id,
                user_id=user_id,
                notification_type=notification_type,
                shard=shard,
                destination=email,
                first_name=first_name,
                last_name=last_name,
                notifications=notifications,
                frontend_url=frontend_url,
                date_time=datetime.datetime.now(tz=tzlocal()),
                data=data,
            )
            if not subject:
                if subject_template:
                    subject = self.renderer.render(mail_data, subject_template)
                else:
                    _logger.warning("no subject found for %s", data)
                    subject = "-"
                body_html = None
                if html_template:
                    body_html = self.renderer.render(mail_data, html_template)
                body_txt = None
                if txt_template:
                    body_txt = self.renderer.render(mail_data, txt_template)
                if body_html is None:
                    if body_txt is None:
                        body_txt = str(mail_data)
                self.sender.send(
                    destination=email,
                    from_address=from_address,
                    subject=subject,
                    body_html=body_html,
                    body_txt=body_txt,
                )
